# -*- coding:utf-8 -*-

import selenium.webdriver
from selenium.webdriver.support.ui import Select
from time import sleep, strftime, strptime
from extScript.UploadPic import UploadPic
from extScript.ClickTimeControl import ClickTimeControl

import os, datetime


class ZJD:
    def __init__(self):
        pass

    def zjd(self, driver, dist=None):
        Select(driver.find_element_by_id("immovablesClass")).select_by_visible_text(u"宅基地使用权")
        driver.find_element_by_id("immovablesDetail").clear()
        driver.find_element_by_id("immovablesDetail").send_keys(dist["不动产具体描述"])
        Select(driver.find_element_by_id("immovablesGetWay")).select_by_visible_text(dist["不动产取得方式"])
        driver.find_element_by_id("immovablesLoc").clear()
        driver.find_element_by_id("immovablesLoc").send_keys(dist["不动产坐落"])
        driver.find_element_by_id("immovablesArea").clear()
        driver.find_element_by_id("immovablesArea").send_keys(dist["面积"])

        if dist["证件类型"]=="不动产证号":
            driver.find_element_by_id("immovablesNo").clear()
            driver.find_element_by_id("immovablesNo").send_keys(dist["不动产证号"])
        else:
            driver.find_element_by_id("homesteadNo").clear()
            driver.find_element_by_id("homesteadNo").send_keys(dist["宅基地使用权证编号"])

        Select(driver.find_element_by_id("circulationType1")).select_by_visible_text(dist["流转性质"])
        driver.find_element_by_id("homesteadeastTo").clear()
        driver.find_element_by_id("homesteadeastTo").send_keys(str(dist["东"]))
        driver.find_element_by_id("homesteadwestTo").clear()
        driver.find_element_by_id("homesteadwestTo").send_keys(str(dist["西"]))
        driver.find_element_by_id("homesteadsouthTo").clear()
        driver.find_element_by_id("homesteadsouthTo").send_keys(str(dist["南"]))
        driver.find_element_by_id("homesteadnorthTo").clear()
        driver.find_element_by_id("homesteadnorthTo").send_keys(str(dist["北"]))

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("circulationType1"))

        ctc = ClickTimeControl(driver, str(dist["开始时间"])[0:10])
        # 点击开始控件
        driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[10]/div[5]/div[3]/input").click()

        # 选择开始时间
        ctc.click_year("/html/body/div[5]/div[5]/table/tbody/tr/td/span")
        ctc.click_month("/html/body/div[5]/div[4]/table/tbody/tr/td/span")
        ctc.click_day("/html/body/div[5]/div[3]/table/tbody/tr")

        ctc = ClickTimeControl(driver, str(dist["截止时间"])[0:10])
        # 点击结束控件
        driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[10]/div[5]/div[5]/input").click()

        # 选择结束时间
        ctc.click_year("/html/body/div[6]/div[5]/table/tbody/tr/td/span")
        ctc.click_month("/html/body/div[6]/div[4]/table/tbody/tr/td/span")
        ctc.click_day("/html/body/div[6]/div[3]/table/tbody/tr")

        driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[10]/div[6]/div[1]").click()

        driver.find_element_by_name("ruralResidenceLand.authorization").click()

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step2"))

        UploadPic(driver, "/html/body/content/div[1]/form/div/div[10]/div[8]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["《流转合同》电子件或照片"]).upfile(
            "/html/body/content/div[1]/form/div/div[10]/div[8]/div[3]/div[1]/div[1]/div[2]/a")

        sleep(1)
        driver.find_element_by_xpath("/html/body/content/div[1]/form/div/div[17]/div[3]/input").click()

        UploadPic(driver, "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["利害关系人意见证明材料"]).upfile(
            "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/a")

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step2"))
        driver.find_element_by_id("textarea2").clear()
        driver.find_element_by_id("textarea2").send_keys(u"备注")
        driver.find_element_by_id("next-step2").click()
